﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YunHan.BuShi123.BLL;
using YunHan.BuShi123.Model;
using System.Data.SqlClient;
using FineUI;

public partial class admin_ad_Edit : AdminBasePage
{

    public int MID { get; set; }

    public int status { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request["status"]))
        {
            status = Convert.ToInt32(Request["status"]);

        }
        else
        {
            FineUI.Alert.Show("参数错误");
            Response.End();
        }
        if (status == 2)
        {
            if (!string.IsNullOrEmpty(Request["MID"]))
            {
                MID = Convert.ToInt32(Request["MID"]);
            }
            else
            {
                FineUI.Alert.Show("参数错误");
                Response.End();
            }

            if (!IsPostBack)
            {
                bind();
            }

        }
    }


    /// <summary>
    /// 提交
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(tbxName.Text.Trim()))
        {
            Alert.Show("名称不能为空");
            return;
        }
        if (string.IsNullOrEmpty(imgPhoto.ImageUrl))
        {
            Alert.Show("请选择上传的Logo");
            return;
        }
        if (string.IsNullOrEmpty(tbxurl.Text.Trim()))
        {
            Alert.Show("链接不能为空");
            return;
        }
        try
        {
            if (string.IsNullOrEmpty(tbxpx.Text.Trim()))
            {
                Alert.Show("链接不能为空");
                return;
            }
            else if (Convert.ToInt32(tbxpx.Text.Trim()) > 10000 || Convert.ToInt32(tbxpx.Text.Trim()) < 1)
            {
                Alert.Show("排序值无效，最小1，最大10000，越大在网页则排越前面");
                return;
            }
        }
        catch (Exception)
        {
            Alert.Show("排序只能输入数值");
            return;
        }


        if (status == 1)
        {
            Friendship_Model Gem = new Friendship_Model()
            {
                Name = tbxName.Text.Trim(),
                Logo = imgPhoto.ImageUrl,
                url = tbxurl.Text.Trim(),
                Sort = Convert.ToInt32(tbxpx.Text.Trim()),
                Remarks = tbxbz.Text.Trim()
            };

            int Result = Friendship_BLL.Add(Gem);
            if (Result > 0)
            {
                Alert.ShowInParent("提交成功");
                FineUI.PageContext.RegisterStartupScript(FineUI.ActiveWindow.GetHideRefreshReference());

            }
            else
            {
                FineUI.Alert.Show("提交失败");
            }
        }
        else if (status == 2)
        {
            int Result = Friendship_BLL.Update("name=@name,logo=@logo,url=@url,sort=@sort,Remarks=@Remarks", "id=@id"
                , new SqlParameter("@name", tbxName.Text.Trim())
                ,new SqlParameter("@logo",imgPhoto.ImageUrl)
                , new SqlParameter("@url", tbxurl.Text.Trim())
                , new SqlParameter("@sort", tbxpx.Text.Trim())
                , new SqlParameter("@Remarks", tbxbz.Text.Trim())
                , new SqlParameter("@id", MID)
                );

            if (Result > 0)
            {

                FineUI.PageContext.RegisterStartupScript(FineUI.ActiveWindow.GetHideRefreshReference());
                FineUI.Alert.Show("提交成功");
            }
            else
            {
                FineUI.Alert.Show("提交失败");
            }

        }
    }
    /// <summary>
    /// 上传Logo
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    #region 上传Logo
    protected void tupan(object sender, EventArgs e)
    {

        if (filePhoto.HasFile)
        {
            string filePath = Server.MapPath("~/upload/");  //E:\xinwenshipin\XinYuanShiPin\XinYuanShiPin\Web\upload
            string fileName = DateTime.Now.Ticks.ToString() + new Random().Next(99999).ToString().PadLeft(5, '0') + ".jpg";
            //while (System.IO.File.Exists(filePath + fileName))
            //{
            //    fileName = DateTime.Now.Ticks.ToString() + new Random().Next(99999).ToString().PadLeft(5, '0') + ".jpg";
            //}

            filePhoto.SaveAs(filePath + fileName);

            imgPhoto.ImageUrl = "/upload/" + fileName;

            //磁盘路径
            //URL
            //相对路径
        }
    }
    #endregion

    /// <summary>
    /// 编辑状态下绑定信息
    /// </summary>
    protected void bind()
    {
        Friendship_Model CM = Friendship_BLL.GetModel("id = @id", new SqlParameter("@id", MID));
        if (CM != null)
        {
            tbxName.Text = CM.Name;//名称
            //imgPhoto.ImageUrl = CM.Logo;//头像
            tbxurl.Text = CM.url;
            tbxpx.Text = CM.Sort.ToString();
            tbxbz.Text = CM.Remarks;
            imgPhoto.ImageUrl = CM.Logo;
        }
    }
}
